﻿using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
//using GHIElectronics.NETMF.USBHost;
using PTP.Core;
using PTP.Devices;
using PTP.Usb.GHI;


namespace Example.GHI.Simple
{

  /// <summary>
  /// This example uses only mandatory PTP commands and should work with every camera.
  /// The program should print to debug output something like this:
  /// Camera type: PTP.Devices.CanonEOS
  /// Number of storages: 1
  /// StorageID: 131073
  /// VolumeLabel: 
  /// StorageDescription: SD
  /// </summary>
  public class Program
  {
    static IPTPDevice camera = null;

    public static void Main()
    {
      // Uncomment to see received bytes!
      //PTP.Core.DebugUtil.WriteMessage += new DebugWriter(DebugUtil_WriteMessage);

      //start listening USB devices
      ListenUsbDevices();

      //put main thread to sleep
      Thread.Sleep(Timeout.Infinite);
    }

    static void DebugUtil_WriteMessage(string category, string message)
    {
      if (category == DebugUtil.ReadBytesCategory)
        Debug.Print(message);
    }


    public static void ListenUsbDevices()
    {
      IConnectionManager cm = GHIConnectionManager.Instance;
      IPTPDevice[] list = cm.GetCurrentDevices();
      if (list.Length > 0)
        camera = list[0];

      cm.DeviceConnected += new PTPDeviceEvent(deviceConnected);
    }

    static void deviceConnected(IPTPDevice device)
    {
      camera = device;
      Debug.Print("Camera type: "+device.GetType());
      uint[] storageIDs = camera.GetStorageIDs();
      Debug.Print("Number of storages: " + storageIDs.Length);
      foreach (uint storageID in storageIDs)
      {
        Debug.Print("StorageID: " + storageID);
        StorageInfo info = camera.GetStorageInfo(storageID);
        Debug.Print("VolumeLabel: "+info.VolumeLabel);
        Debug.Print("StorageDescription: " + info.StorageDescription);
      }
	  camera.InitiateCapture();
    }


  }
}
